var util = require('../../../utils/util.js');
var wxcache = require('../../../utils/wxcache.js');
var api = require('../../../config/api.js');
var user = require('../../../services/user.js');

var app = getApp();

Page({
  data: {
    userInfo: {},
    login_flag: 0,
    isNewPerson: '',
    imageWidth: 0,
    hiddenicon:true,
    topaynumtopaynum:0,
    totripnum:0,
    tocommentnum:0,
    couponnum:0
  },
  onLoad: function (options) {
    
    var that = this;
    that.imageLoad();
  },
  onReady: function () {

  },
  onShow: function () {

    let that = this;

    var phonenumber = wxcache.get("phoneNumber")
    user.checkLogin().then(function (res) {

      //加载小图标
      that.loadicon();

      //登陆了，根据手机号，从后端取信息
      util.request(api.QueryUserInfo, { mobile: phonenumber }).then(function (res) {
       
        if (res.code == 200){
          wxcache.put("userInfo", res.responseBody)

          that.setData({
            login_flag: 1,
            hiddenicon: false,
            userInfo: res.responseBody,
          })
        }else{
          wxcache.remove("userInfo")
          wxcache.remove("phoneNumber") 
          that.setData({
            login_flag: 0,
            hiddenicon: true
          });
        }
      })
    }, function (error) {
     
      that.setData({
        login_flag: 0,
        hiddenicon: true
      });
    }) 
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  //处理右上标
  loadicon:function(){
    var that = this;
    
    //订单数量
    util.request(api.OrdersNum,{bizType:1}).then(res=>{
      // console.log(res)
      var data = res.responseBody
    
      that.setData({
        topaynum:data.waitPayNum,
        totripnum:data.paySuccessNum,
        tocommentnum:data.waitCommentNum
      })
    })

    // 优惠券数量
    util.request(api.CouponsQuery).then(res=>{
      // console.log(res)
      var couponList = []
      res.responseBody.userCouponVoList.forEach((coupon, index) => {
        // console.log(coupon)
        if (coupon.status == "ACTIVATED") {
          couponList.push(coupon)
        }
      })
      that.setData({
        couponnum: couponList.length
      })
    })

  },
  imageLoad: function () {
    this.setData({
      imageWidth: wx.getSystemInfoSync().windowWidth - 15
    })
  },

  goLogin() {
    user.checkLogin().then(function (res) {
      wx.navigateTo({
        url: '/pages/auth/userInfo/userInfo',
      })
    }, function (res) {
      wx.navigateTo({
        url: '/pages/auth/registerAndLogin/registerAndLogin',
      })
    })
  },
  onGotUserInfo: function (e) {

    var that = this;
    console.log(e)
    user.getUserinfoByButton(e).then(res => {
      wx.navigateTo({
        url: '/pages/auth/registerAndLogin/registerAndLogin',
      })
    },function(){
      wx.navigateTo({
        url: '/pages/auth/registerAndLogin/registerAndLogin',
      })
    })
  },

  myPageBind: function (e) {
    var that = this;
    var bindUrl = "";
    if (e.currentTarget.id == 'myorder') {
      bindUrl = '/pages/ucenter/order/order?tab=' + e.currentTarget.dataset.tab;
    } else if (e.currentTarget.id == 'mycollect') {
      bindUrl = '/pages/ucenter/collect/collect';
    } else if (e.currentTarget.id == 'myfootprint') {
      bindUrl = '/pages/ucenter/footprint/footprint';
    } else if (e.currentTarget.id == 'mycoupon') {
      bindUrl = '/pages/ucenter/coupon/coupon';
    } else if (e.currentTarget.id == 'myaccountSecurity') {
      bindUrl = '/pages/ucenter/accountSecurity/accountSecurity';
    } else if (e.currentTarget.id == 'myfeedback') {
      bindUrl = '/pages/ucenter/feedback/feedback';
    } else if (e.currentTarget.id == 'mysetting') {
      bindUrl = '/pages/ucenter/setting/setting';
    }

    user.checkLogin().then(function (res) {
      wx.navigateTo({
        url: bindUrl,
      })
    }, function (error) {
      wx.showModal({
        title: '提示',
        content: '您还未登录，请登录',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    })
  }
 
})